# encoding:utf-8

import unittest
import time
from selenium import webdriver


class Login(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox(executable_path="E:\python_tool\geckodriver")
        self.driver.maximize_window()
        self.driver.get(
            "https://passport.jd.com/new/login.aspx?ReturnUrl=https%3A%2F%2Fwww.jd.com%2F%3Fcu%3Dtrue%26utm_source%3Dbaidu-pinzhuan%26utm_medium%3Dcpc%26utm_campaign%3Dt_288551095_baidupinzhuan%26utm_term%3D0f3d30c8dba7459bb52f2eb5eba8ac7d_0_d95e8aef0a224a01b50d3560c708a097")
        self.driver.implicitly_wait(3)
        pass

    def tearDown(self):
        self.driver.quit()
        pass

    # 点击LOGO跳转至主页
    def test_logo_turn_home(self):
        u"""点击logo跳转至首页"""
        self.img_logo = self.driver.find_element_by_id("logo")
        self.img_logo.click()
        time.sleep(5)
        self.title = self.driver.title
        self.assertEquals(self.title, u"京东(JD.COM)-正品低价、品质保障、配送及时、轻松购物！")
        pass

    # 点击调查问卷链接，跳转至调查问卷页面
    def test_link_survey(self):
        u"""调查问卷链接"""
        self.a_survey = self.driver.find_element_by_link_text("登录页面，调查问卷")
        # 获取当前浏览器的窗口
        current_window = self.driver.current_window_handle
        self.a_survey.click()
        self.title_current_page = self.getCurrentWindowTitle(current_window)
        # self.driver.implicitly_wait(10)
        # all_windows = self.driver.window_handles
        # # for循环遍历浏览器窗口
        # for window in all_windows:
        #     if current_window != window:
        #         # 切换到当前窗口
        #         self.driver.switch_to_window(window)
        #         self.title_home = self.driver.title
        self.assertEquals(self.title_current_page, u"登录页满意度调查")
        pass

    # 点击账户登录，显示账号密码输入框
    def test_account_login(self):
        u"""使用账户登录方式，显示账号密码输入框等"""
        self.account_login = self.driver.find_element_by_link_text("账户登录")
        self.account_login.click()
        self.driver.implicitly_wait(3)
        self.login_box=self.driver.find_element_by_class_name("login-box")
        self.assertTrue(self.login_box.is_displayed())
        pass

    # 点击扫码登录，显示二维码
    def test_qrcode_login(self):
        u"""二维码登录，显示二维码"""
        self.qrcode_login=self.driver.find_element_by_link_text("扫码登录")
        self.qrcode_login.click()
        self.driver.implicitly_wait(3)
        self.qrcode_login=self.driver.find_element_by_class_name("qrcode-login")
        self.assertTrue(self.qrcode_login.is_displayed())
        pass

    # 账户登录，账户名密码为空
    def test_un_em_pw_em(self):
        u"""账户密码为空，点击登录按钮"""
        self.account_login = self.driver.find_element_by_link_text("账户登录")
        self.account_login.click()
        self.getAssert()
        self.assertEquals(self.text_error,u"请输入账户名和密码")
        # 置空断言
        self.text_error=""
        pass

    #账户登录，输入账户，密码为空
    def test_un_noem_pw_em(self):
        u"""账户不为空，密码为空，点击登录"""
        self.account_login = self.driver.find_element_by_link_text("账户登录")
        self.account_login.click()
        self.driver.implicitly_wait(3)
        self.loginname=self.driver.find_element_by_id("loginname")
        self.loginname.send_keys("123123")
        self.getAssert()
        self.assertEquals(self.text_error,u"请输入密码")
        # 置空断言
        self.text_error = ""
        pass

    #使用qq账号登录
    def test_login_qq(self):
        u"""点击qq标志，跳转至qq登录页面"""
        self.link_qq_login=self.driver.find_element_by_xpath("/html/body/div[2]/div/div[1]/div/div[5]/ul/li[1]/a")
        self.link_qq_login.click()
        self.driver.implicitly_wait(10)
        self.assertEquals(self.driver.title,u"QQ帐号安全登录")
        pass

    #使用微信账号登录
    def test_login_wechat(self):
        u"""点击微信标志，跳转至微信登录页面"""
        self.link_wechat_login = self.driver.find_element_by_xpath("/html/body/div[2]/div/div[1]/div/div[5]/ul/li[2]/a")
        self.link_wechat_login.click()
        self.driver.implicitly_wait(10)
        self.assertEquals(self.driver.title, u"微信登录")
        pass

    # 账户登录，账户密码不为空，密码错误
    def test_un_noem_pw_noem_error(self):
        u"""账户密码不为空，密码错误"""
        self.account_login = self.driver.find_element_by_link_text("账户登录")
        self.account_login.click()
        self.driver.implicitly_wait(3)
        self.loginname = self.driver.find_element_by_id("loginname")
        self.loginname.send_keys("15933494836")
        self.nloginpwd=self.driver.find_element_by_id("nloginpwd")
        self.nloginpwd.send_keys("123123")
        self.getAssert()
        self.assertEquals(self.text_error,u"账户名与密码不匹配，请重新输入")
        # 置空断言
        self.text_error = ""
        pass

    # 关于我们 链接
    def test_link_about_me(self):
        u"""点击关于我们跳转至企业介绍页面"""
        self.about_me = self.driver.find_element_by_link_text("关于我们")
        current_window = self.driver.current_window_handle
        self.about_me.click()
        self.title_current_page = self.getCurrentWindowTitle(current_window)
        self.assertEquals(self.title_current_page, u"企业简介-京东商城")
        pass

    # 联系我们 链接
    def test_link_contact_me(self):
        u"""点击联系我们跳转至联系我们页面"""
        self.contact_me = self.driver.find_element_by_link_text("联系我们")
        current_window = self.driver.current_window_handle
        self.contact_me.click()
        self.title_current_page = self.getCurrentWindowTitle(current_window)
        self.assertEquals(self.title_current_page, u"联系我们-京东商城")
        pass

    # 人才招聘 链接
    def test_link_recruit(self):
        u"""点击人才招聘跳转至京东招聘官网页面"""
        self.link_recruit = self.driver.find_element_by_link_text("人才招聘")
        current_window = self.driver.current_window_handle
        self.link_recruit.click()
        self.title_current_page = self.getCurrentWindowTitle(current_window)
        self.assertEquals(self.title_current_page, u"京东招聘官网")
        pass

    # 商家入驻 链接
    def test_link_busines(self):
        u"""点击商家入驻跳转至商家入驻页面"""
        self.link_business = self.driver.find_element_by_link_text("商家入驻")
        current_window = self.driver.current_window_handle
        self.link_business.click()
        self.title_current_page = self.getCurrentWindowTitle(current_window)
        self.assertEquals(self.title_current_page, u"商家入驻-入驻流程 - 京东全品类专题活动-京东")
        pass


    # 广告服务 链接
    def test_link_advertisement(self):
        u"""点击广告服务跳转至广告服务页面"""
        self.link_advertisement = self.driver.find_element_by_link_text("广告服务")
        current_window = self.driver.current_window_handle
        self.link_advertisement.click()
        self.title_current_page = self.getCurrentWindowTitle(current_window)
        self.assertEquals(self.title_current_page, u"广告服务 - 京东全品类专题活动-京东")
        pass

    # 手机京东 链接
    def test_link_mobli_jd(self):
        u"""点击手机京东跳转至手机京东页面"""
        self.link_mobil_jd = self.driver.find_element_by_link_text("手机京东")
        current_window = self.driver.current_window_handle
        self.link_mobil_jd.click()
        self.title_current_page = self.getCurrentWindowTitle(current_window)
        self.assertEquals(self.title_current_page, u"手机京东 - 京东")
        pass

    # 友情链接 链接
    def test_link_friendly(self):
        u"""点击友情链接跳转至友情链接页面"""
        self.link_friendly = self.driver.find_element_by_link_text("友情链接")
        current_window = self.driver.current_window_handle
        self.link_friendly.click()
        self.title_current_page = self.getCurrentWindowTitle(current_window)
        self.assertEquals(self.title_current_page, u"友情链接")
        pass

    # 销售联盟 链接
    def test_link_union(self):
        u"""点击销售联盟跳转至京东联盟页面"""
        self.link_union = self.driver.find_element_by_link_text("销售联盟")
        current_window = self.driver.current_window_handle
        self.link_union.click()
        self.title_current_page = self.getCurrentWindowTitle(current_window)
        self.assertEquals(self.title_current_page, u"京东联盟")
        pass

    # 京东社区 链接
    def test_link_union(self):
        u"""点击京东社区跳转至京东社区页面"""
        self.link_union = self.driver.find_element_by_link_text("京东社区")
        current_window = self.driver.current_window_handle
        self.link_union.click()
        self.title_current_page = self.getCurrentWindowTitle(current_window)
        self.assertEquals(self.title_current_page, u"京东社区 - 京东电脑、办公|游戏设备专题活动-京东")
        pass

    # 京东公益 链接
    def test_link_public_welfare(self):
        u"""点击京东公益跳转至京东公益页面"""
        self.link_public_welfare = self.driver.find_element_by_link_text("京东公益")
        current_window = self.driver.current_window_handle
        self.link_public_welfare.click()
        self.title_current_page = self.getCurrentWindowTitle(current_window)
        self.assertEquals(self.title_current_page, u"京东公益")
        pass

    # 账户登录，获取断言的相同部分
    def getAssert(self):
        self.driver.implicitly_wait(3)
        self.loginsubmit = self.driver.find_element_by_id("loginsubmit")
        self.loginsubmit.click()
        self.driver.implicitly_wait(3)
        self.msg_error = self.driver.find_element_by_class_name("msg-error")
        self.text_error = self.msg_error.text
        pass

    # 循环浏览器窗口取得当前title
    def getCurrentWindowTitle(self, current_window):
        self.driver.implicitly_wait(10)
        all_windows = self.driver.window_handles
        # for循环遍历浏览器窗口
        for window in all_windows:
            if current_window != window:
                # 切换到当前窗口
                self.driver.switch_to_window(window)
                self.title_current_page = self.driver.title
                return self.title_current_page
